
<template>
  <div class="accountList">
    <div class="searchBox">
      <el-form :model="searchForm" ref="searchFormRef">
        <el-row>
          <el-form-item prop="title">
            <div class="item">
              <span v-html="'动态标题'" class="nameSpan"></span>
              <el-input
                size="mini"
                v-model="searchForm.title"
                placeholder="请输入动态标题"
              ></el-input>
            </div>
          </el-form-item>

          <el-form-item prop="phone">
            <div class="item">
              <span v-html="'发布人账号'" class="nameSpan"></span>
              <el-input
                size="mini"
                v-model="searchForm.phone"
                placeholder="请输入发布人账号"
              ></el-input>
            </div>
          </el-form-item>
          <el-form-item prop="auditStatus">
            <div class="item">
              <span v-html="'审核状态'" class="nameSpan"></span>
              <el-select filterable 
                clearable
                size="mini"
                v-model="searchForm.auditStatus"
                placeholder="请选择审核状态"
              >
                <el-option
                  v-for="item in statusOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </div>
          </el-form-item>
          <el-form-item prop="publishPlatform">
            <div class="item">
              <span v-html="'发布渠道'" class="nameSpan"></span>
              <el-select filterable 
                clearable
                size="mini"
                v-model="searchForm.publishPlatform"
                placeholder="请选择发布渠道"
              >
                <el-option
                  v-for="item in publishPlatformOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </div>
          </el-form-item>
          <el-form-item prop="posterTopicId">
            <div class="item">
              <span v-html="'选择话题'" class="nameSpan"></span>
              <el-select filterable 
                clearable
                size="mini"
                v-model="searchForm.posterTopicId"
                placeholder="请选择选择话题"
              >
                <el-option
                  v-for="item in topicOptions"
                  :key="item.id"
                  :label="item.topicName"
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </div>
          </el-form-item>
          <el-form-item>
            <el-button
              @click="searchClick"
              icon="el-icon-search"
              type="primary"
              size="mini"
              >查询</el-button
            >
            <el-button icon="el-icon-refresh" size="mini" @click="resetClick"
              >重置</el-button
            >
          </el-form-item>
        </el-row>
      </el-form>
    </div>
    <div class="topBtn">
      <el-button
        size="mini"
        @click="consentClick"
        type="success"
        v-has="'community:poster:audit'"
        >审核通过</el-button
      >
      <el-button
        size="mini"
        @click="refuseClick"
        type="danger"
        v-has="'community:poster:audit'"
        >审核不通过</el-button
      >
      <el-button
        size="mini"
        @click="delClick"
        type="primary"
        v-has="'community:poster:delete'"
        >删除</el-button
      >
    </div>
    <div class="tableBox">
      <el-table
       ref="tableBoxRef"
        v-loading="tableLoading"
        :header-cell-style="{
          background: '#F8F8F8',
          color: '#333',
          fontSize: '16px',
          fontWeight: '700',
        }"
        :height="605"
        border
        :data="tableData"
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" > </el-table-column>
        <el-table-column
          prop="title"
          align="center"
          label="动态标题"
           
        >
        </el-table-column>
        <el-table-column
          prop="publishPlatform"
          align="center"
          label="发布渠道"
           
        >
          <template slot-scope="scope">
            <span>{{ publishFormatter[scope.row.publishPlatform] }}</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="userName"
          align="center"
          label="发布人昵称"
           
        >
        </el-table-column>
          <el-table-column
              prop="phone"
              align="center"
              label="发布人账号"
               
            >
            </el-table-column>
        <el-table-column
          prop="createTime"
          align="center"
          label="发布时间"
           
        >
          <template slot-scope="scope">{{
            scope.row.createTime | formatDate
          }}</template>
        </el-table-column>
        <el-table-column
          sortable
          prop="viewCount"
          align="center"
          label="浏览量"
           
        >
         <template slot-scope="scope">
            <div v-if="scope.row.viewCount != null">
              {{ scope.row.viewCount }}
            </div>
            <div v-else>-</div>
          </template>
        </el-table-column>
        <el-table-column
          sortable
          prop="goodCount"
          align="center"
          label="点赞数"
           
        >
          <template slot-scope="scope">
            <div v-if="scope.row.goodCount != null">
              {{ scope.row.goodCount }}
            </div>
            <div v-else>-</div>
          </template>
        </el-table-column>
        <el-table-column
          sortable
          prop="commentCount"
          align="center"
          label="评论数"
           
        >
          <template slot-scope="scope">
            <div v-if="scope.row.commentCount != null">
              {{ scope.row.commentCount }}
            </div>
            <div v-else>-</div>
          </template>
        </el-table-column>
        <el-table-column
          sortable
          prop="favorCount"
          align="center"
          label="关注数"
           
        >
          <template slot-scope="scope">
            <div v-if="scope.row.favorCount != null">
              {{ scope.row.favorCount }}
            </div>
            <div v-else>-</div>
          </template>
        </el-table-column>
        <el-table-column
          prop="auditStatus"
          align="center"
          label="审核状态"
           
        >
          <template slot-scope="scope">
            <div
              :style="{
                color:
                  scope.row.auditStatus == 1
                    ? '#67c33e'
                    : scope.row.auditStatus == 2
                    ? '#f16f6d'
                    : '',
              }"
            >
              {{ auditormatter[scope.row.auditStatus] }}
            </div>
          </template>
        </el-table-column>
        <el-table-column
          prop="auditTime"
          align="center"
          label="审核时间"
           
        >
          <template slot-scope="scope">
            <div v-if="scope.row.auditTime != null">
              {{ scope.row.auditTime | formatDate }}
            </div>
            <div v-else>-</div>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              icon="el-icon-edit-outline"
              size="mini"
              type="text"
              v-has="'community:poster:edit'"
              @click="editClick(scope.row,scope.$index)"
              >编辑</el-button
            >
            <el-button
              icon="el-icon-edit-outline"
              size="mini"
              type="text"
              v-has="'community:poster:info'"
              @click="detailClick(scope.row)"
              >详情</el-button
            >
            <el-button
              v-if="scope.row.auditStatus == 0"
              icon="el-icon-notebook-1
"
              size="mini"
              type="text"
              v-has="'community:poster:audit'"
              @click="auditClick(scope.row)"
              >审核</el-button
            >
            <el-button
              v-if="scope.row.auditStatus == 1"
              icon="el-icon-document"
              size="mini"
              type="text"
              @click="changeClick(scope.row)"
              >查看评论</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <el-card class="footer" v-if="total > 0">
         <div>
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-sizes="[15, 30, 50, 100]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
      </el-card>
    </div>

    <!-- 新增/修改权限 -->
    <el-dialog :close-on-click-modal="false" 
      :title="title"
      :visible.sync="addEditDialogVisible"
      width="500px"
      @open="addEditDialogOpen"
      class="addEditAccountDialog"
    >
      <div class="content">
        <el-form
          :model="addEditForm"
          :rules="addEditRules"
          ref="addEditFormRef"
        >
          <el-form-item prop="title" label="标题：">
            <div class="item">
              <el-input
                size="mini"
                v-model="addEditForm.title"
                placeholder="请输入标题"
                maxlength="200"
              >
              </el-input>
            </div>
          </el-form-item>
          <el-form-item prop="content" label="内容：">
            <div class="item">
              <el-input
                type="textarea"
                :rows="3"
                size="mini"
                v-model="addEditForm.content"
                placeholder="请输入内容"
              >
              </el-input>
            </div>
          </el-form-item>
        </el-form>
      </div>
      <div class="btns">
        <el-button size="mini" @click="addEditDialogVisible = false"
          >取 消</el-button
        >
        <el-button
          v-clickForbidden
          size="mini"
          type="primary"
          @click="addEditDialogSaveClick"
          >确 定</el-button
        >
      </div>
    </el-dialog>
    <el-dialog :close-on-click-modal="false" 
      :title="detailTitle"
      :visible.sync="dialogVisible"
      width="1100px"
      class="addEditAccountDialog"
    >
      <div class="content-detail">
        <div class="content-style">
          <div class="content-item">
            <span style="font-size: 16px"> 标题：</span
            ><span>{{ detailForm.title }}</span>
          </div>
          <div class="content-item">
            <span style="font-size: 16px"> 内容：</span
            ><span>{{ detailForm.content }}</span>
          </div>
           <div style="display: flex; flex-wrap: wrap" class="content-item">
          <div v-for="(item, index) in imgUrl" :key="index">
            <el-image
              style="
                width: 200px;
                display: flex;
                margin-left: 10px;
                margin-bottom: 20px;
              "
              :src="item"
              fit="contain"
              :preview-src-list="[item]"
              ></el-image
            >
          </div>
        </div>
        </div>
        <div class="content-table">
          <el-table
            :header-cell-style="{
              background: '#F8F8F8',
              color: '#333',
              fontSize: '16px',
              fontWeight: '700',
            }"
            border
            :data="detailData"
            style="width: 100%"
          >
            <el-table-column
              prop="publishPlatform"
              align="center"
              label="发布渠道"
               
            >
              <template slot-scope="scope">
                <span>{{ publishFormatter[scope.row.publishPlatform] }}</span>
              </template>
            </el-table-column>
            <el-table-column
              prop="userName"
              align="center"
              label="发布人昵称"
               
            >
            </el-table-column>
            <el-table-column
              prop="phone"
              align="center"
              label="发布人账号"
               
            >
            </el-table-column>
            <el-table-column
              prop="createTime"
              align="center"
              label="发布时间"
               
            >
              <template slot-scope="scope">{{
                scope.row.createTime | formatDate
              }}</template>
            </el-table-column>
            <el-table-column
              prop="viewCount"
              align="center"
              label="浏览量"
               
            >
            </el-table-column>
            <el-table-column
              prop="goodCount"
              align="center"
              label="点赞数"
               
            >
              <template slot-scope="scope">
                <div v-if="scope.row.goodCount != null">
                  {{ scope.row.goodCount }}
                </div>
                <div v-else>-</div>
              </template>
            </el-table-column>
            <el-table-column
              prop="commentCount"
              align="center"
              label="评论数"
               
            >
              <template slot-scope="scope">
                <div v-if="scope.row.commentCount != null">
                  {{ scope.row.commentCount }}
                </div>
                <div v-else>-</div>
              </template>
            </el-table-column>
            <el-table-column
              prop="favorCount"
              align="center"
              label="关注数"
               
            >
              <template slot-scope="scope">
                <div v-if="scope.row.favorCount != null">
                  {{ scope.row.favorCount }}
                </div>
                <div v-else>-</div>
              </template>
            </el-table-column>
            <el-table-column
              prop="auditStatus"
              align="center"
              label="审核状态"
               
            >
              <template slot-scope="scope">
                <div
                  :style="{
                    color:
                      scope.row.auditStatus == 1
                        ? '#67c33e'
                        : scope.row.auditStatus == 2
                        ? '#f16f6d'
                        : '',
                  }"
                >
                  {{ auditormatter[scope.row.auditStatus] }}
                </div>
              </template>
            </el-table-column>
            <el-table-column
              prop="auditTime"
              align="center"
              label="审核时间"
               
            >
              <template slot-scope="scope">
                <div v-if="scope.row.auditTime != null">
                  {{ scope.row.auditTime | formatDate }}
                </div>
                <div v-else>-</div>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div v-if="detailTitle == '审核'"
         class="audit-btns">
          <el-button type="danger" @click="auditDetail(2)"
            >审核不通过</el-button
          >
          <el-button
           type="success"
            @click="auditDetail(1)"
            style="margin-left: 30px"
            >审核通过</el-button
          >
        </div>
      </div>
      <div class="btns">
        <el-button size="mini" @click="dialogVisible = false">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
export default {
  data() {
    return {
      imgUrl:[],
      multipleSelection: [],
      topicOptions: [],
      statusOptions: [
        { label: "待审核", value: 0 },
        { label: "审核通过", value: 1 },
        { label: "审核不通过", value: 2 },
      ],
      publishFormatter: {
        0: "pc",
        1: "小程序",
      },
      auditormatter: {
        0: "待审核",
        1: "审核通过",
        2: "审核不通过",
      },
      publishPlatformOptions: [
        { label: "pc", value: 0 },
        { label: "小程序", value: 1 },
      ],
      searchForm: {
        title: "",
        phone: "",
        auditStatus: "",
        publishPlatform: "",
        posterTopicId: "",
      },
      stautsOptions: [
        { label: "正常", value: 1 },
        { label: "禁用", value: 2 },
      ],
      tableLoading: false,
      tableHeight: 50,
      tableData: [],

      pageNum: 1,
      pageSize: 15,
      total: 0,
      addEditDialogVisible: false,
      dialogVisible: false,
      detailData: [],
      detailForm: {
        
      },
      addEditForm: {
        content: "",
        title: "",
      },
      addEditRules: {
        title: [{ required: true, message: "标题不能为空", trigger: "blur" }],
        content: [{ required: true, message: "内容不能为空", trigger: "blur" }],
      },
      roleOptions: [],
      title: "新增 ",
      detailTitle: "",
      editId: "",
      detailId: "",
    };
  },
  computed: {
    ...mapGetters(["dictList"]),
  },
  created() {
    this.getAuditList();
  },
  methods: {
    consentClick() {
        this.tableData.forEach((item,index)=>{
            this.multipleSelection.forEach((itemChild,indexChild)=>{
              if(item.id==itemChild){
                if(item.auditStatus!=0){
                  this.multipleSelection.splice(indexChild, 1)
                }
              }
            })
        })
     
      if (this.multipleSelection.length > 0) {
        this.$confirm(
          "通过后，该条动态将同步在PC及小程序端显示，确认通过吗?",
          "提示",
          {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          }
        )
          .then(() => {
            let data = {
              posterIds: this.multipleSelection,
              auditStatus: 1,
            };
            this.$api.setAudit(data).then((res) => {
              if (res.data.code == "00000") {
                this.$message.success("批量审核通过成功");
                this.getAuditList()
              }
            });
          })
          .catch(() => {});
      } else {
        this.$message.warning("请选择待审核的动态");
        this.$refs.tableBoxRef.clearSelection()
      }
    },
    refuseClick() {
      console.log(this.multipleSelection);
     this.tableData.forEach((item,index)=>{
            this.multipleSelection.forEach((itemChild,indexChild)=>{
              if(item.id==itemChild){
                if(item.auditStatus!=0){
                  this.multipleSelection.splice(indexChild, 1)
                }
              }
            })
        })
      if (this.multipleSelection.length > 0) {
        this.$confirm(
          "若审核不通过，该条动态仅发布者可见。确认不通过吗?",
          "提示",
          {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          }
        )
          .then(() => {
            let data = {
              posterIds: this.multipleSelection,
              auditStatus: 2,
            };
            this.$api.setAudit(data).then((res) => {
              if (res.data.code == "00000") {
                this.$message.success("批量审核不通过成功");
                 this.getAuditList()
              }
            });
          })
          .catch(() => {});
      } else {
        this.$message.warning("请选择待审核的动态");
         this.$refs.tableBoxRef.clearSelection()
        
      }
    },
    delClick() {
      if (this.multipleSelection.length > 0) {
        this.$confirm(
          "删除后，PC端和小程序同步删除这些动态，且无法恢复。确认要删除吗?",
          "提示",
          {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          }
        )
          .then(() => {
            this.$api
              .delAudit({ posterIds: this.multipleSelection })
              .then((res) => {
                if (res.data.code == "00000") {
                  this.$message.success("批量删除成功");
                  this.getAuditList()
                }
              });
          })
          .catch(() => {});
      } else {
        this.$message.warning("请选择待删除的动态");
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val.map(({ id }) => id);
    },
    selectable(row){
      // if(row.auditStatus == 0){
      //    return true;  
      // }
    },
    // 详情
    detailClick(row) {
      this.detailTitle = "详情 ";
      this.dialogVisible = true;
      this.detailData = [];

      this.$nextTick(() => {
        this.$api.auditDetail(row.id).then((res) => {
          if (res.data.code == "00000") {
            this.detailForm = res.data.result;
            let arr = JSON.parse(JSON.stringify(this.detailForm));
            this.detailData.push(arr);
             this.detailData.imgUrl =
            this.imgUrl = this.detailData.imgUrl.split(",");
          }
        });
      });
    },
     async  auditDetail(status){
     const confirm = await this.$confirm(
          '确定执行当前操作吗？',
          "提示",
          {
            confirmButtonText: "确认",
            cancelButtonText: "取消",
            type: "warning",
          }
        ).catch(() => false);
        if (!confirm) return false;
         let data = {
              posterIds: [this.detailId],
              auditStatus: status,
            };
          const res = await this.$api.setAudit(data);
          if (res.data.code == "00000") {
            this.$message.success("操作成功");
               this.getAuditList()
               this.dialogVisible=false
          }
    },
    // 审核
    auditClick(row) {
      this.detailId=''
      this.detailTitle = "审核";
      this.dialogVisible = true;
    this.detailId=row.id
      this.detailData = [];
      this.$nextTick(() => {
        this.$api.auditDetail(row.id).then((res) => {
          if (res.data.code == "00000") {
            this.detailForm = res.data.result;
            let arr = JSON.parse(JSON.stringify(this.detailForm));
            this.detailData.push(arr);
            this.detailData.imgUrl =
            this.imgUrl = this.detailData.imgUrl.split(",");
          }
        });
      });
    },
    // 查看评论
    changeClick(row) {
       this.$router.push({
        path: "/communityManagement/changeComment",
        query: { id: row.id, title: "查看评论" },
      });
    },

    resetClick() {
      this.$refs.searchFormRef.resetFields();
      this.pageNum = 1;
      this.getAuditList();
    },
    searchClick() {
      this.pageNum = 1;
      this.getAuditList();
    },

    /**
     * 获取列表
     * @param {*}
     */
    async getAuditList() {
      this.tableLoading = true;
      const res = await this.$api.auditList({
        pageSize: this.pageSize,
        pageNum: this.pageNum,
        ...this.searchForm,
      });
      this.total = res.data.result.total;
      this.tableData = res.data.result.list;
      this.getTopicList();
      this.tableLoading = false;
    },
    async getTopicList() {
      await this.$api
        .topicList({
          showStatus: 1,
        })
        .then((res) => {
          this.topicOptions = res.data.result;
        });
    },

    /**
     * 编辑按钮
     * @param {*} row
     */
    async editClick(row, index) {
      this.title = "编辑 ";
      this.addEditDialogVisible = true;
      this.editId = row.id;

      this.$nextTick(() => {
        this.addEditForm = JSON.parse(JSON.stringify(this.tableData[index]));
      });
    },

    /**
     * 切换页面大小
     * @param {*} val
     */
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageSize = val;
      this.getAuditList();
    },

    /**
     * 页码切换
     * @param {*} val
     */
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pageNum = val;
      this.getAuditList();
    },

    /**
     * 打开修改的回调
     * @param {*}
     */
    addEditDialogOpen() {
      if (this.$refs.addEditFormRef) this.$refs.addEditFormRef.resetFields();
    },

    /**
     * 编辑保存按钮
     * @param {*}
     */
    addEditDialogSaveClick() {
      this.$refs.addEditFormRef.validate(async (boolean) => {
        if (boolean) {
          let data = {
            posterIds: [this.editId],
            ...this.addEditForm,
          };
          const res = await this.$api.editAudit(data);
          if (res.data.code == "00000") {
            this.$message.success("编辑成功");
            this.getAuditList();
            this.addEditDialogVisible = false;
          }
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
.accountList {
  height: 100%;
  display: flex;
  flex-direction: column;
  padding: 20px;
  .searchBox {
    .el-row {
      display: flex;
      flex-wrap: wrap;
      .el-form-item {
        margin-bottom: 0;
      }
      .item {
        display: flex;
        align-items: center;
        margin-right: 20px;
        .nameSpan {
          padding-right: 10px;
          box-sizing: border-box;
          white-space: nowrap;
        }
        .el-input {
          width: 162px;
        }
      }
    }
  }
   .topBtn {
    margin: 10px 0;
text-align: right;
  }

  .addEditAccountDialog {
    .el-form-item {
      .el-select {
        width: 100%;
      }
    }
    .btns {
      text-align: right;
      margin-bottom: 0;
    }
  }
}
/deep/.el-button--success.is-plain {
  color: #fff;
  border-color: #409eff;
  background-color: #409eff;
}
/deep/.el-form-item {
  margin-bottom: 12px;
}
.content {
  height: 30vh;
  overflow-x: hidden;
}
.content-detail {
  height: 60vh;
  overflow-x: hidden;
  .content-style {
    background-color: #efefef;
    padding: 20px;
    border-bottom: 1px solid #797979;
    .content-item {
      margin-bottom: 20px;
    }
  }
  .content-table {
    padding: 20px;
  }
}
.audit-btns {
  margin-top: 40px;
  text-align: center;
}

</style>